
package code.codeworld.search.web_search.service.impl;

import code.codeworld.common.kits.EditTextUtil;
import code.codeworld.record.browse_record.model.BrowseRecord;
import code.codeworld.record.browse_record.service.BrowseRecordService;
import code.codeworld.search.web_search.mapper.WebSearchMapper;
import code.codeworld.search.web_search.model.constant.WebSearchConstant;
import code.codeworld.search.web_search.model.enums.WebSearchEnum;
import code.codeworld.search.web_search.model.vo.WebSearchResultVo;
import code.codeworld.search.web_search.model.vo.WebSearchVo;
import code.codeworld.search.web_search.service.WebSearchService;
import code.codeworld.common.kits.PageUtil;
import code.codeworld.common.kits.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * WebSearchService接口实现类
 *
 * @author freecode
 * @date 2023/03/23 00:20
 **/
@Service
@Slf4j
public class WebSearchServiceImpl implements WebSearchService {

    @Resource
    private BrowseRecordService browseRecordService;
    @Resource
    private WebSearchMapper webSearchMapper;

    @Override
    public Page<WebSearchResultVo> search(WebSearchVo webSearchVo) {
        Integer[] pageLimits = new Integer[]{webSearchVo.getPageNum(), webSearchVo.getPageSize()};
        PageUtil.getPageLimits(pageLimits);
        webSearchVo.setPageNum(pageLimits[0]);
        webSearchVo.setPageSize(pageLimits[1]);
        //全局查询
        Page<WebSearchResultVo> page = new Page<WebSearchResultVo>();
        int count = webSearchMapper.globalSearchCount(webSearchVo);
        if (count > BigDecimal.ROUND_UP) {
            List<WebSearchResultVo> data = webSearchMapper.globalSearch(webSearchVo);
            int autoNum = pageLimits[0];
            for (WebSearchResultVo t : data) {
                //序号自增
                t.setAutoNum(++autoNum);
                //简介过滤html符号
                t.setBlurb(EditTextUtil.getText(t.getBlurb()));
                /*//截取简介长度到300
                int length = 300;
                if (t.getBlurb().length() > length) {
                    t.setBlurb(t.getBlurb().substring(BigDecimal.ROUND_UP, length) + "..");
                }*/
                //对应模块名称 模块地址
                WebSearchEnum.ModuleNameEnum Enum = WebSearchEnum.ModuleNameEnum.getEnum(t.getHref());
                if (!Objects.isNull(Enum)) {
                    t.setModuleName(Enum.getName());
                    t.setHref(Enum.getHref());
                }
                //图片以"\"符号分组，取第一
                if (t.getImages() != null && !t.getImages().isEmpty()) {
                    String[] images = t.getImages().split("\\\\");
                    t.setImages(images[0]);
                }
            }
            page.setRecords(data);
        }
        page.setTotal(count);
        //延时执行--添加浏览记录
        new Timer().schedule(new TimerTask() {
            public void run() {
                log.info("延时执行--添修浏览记录开始：" + System.currentTimeMillis());
                BrowseRecord browseRecord = new BrowseRecord();
                browseRecord.setKeyword(webSearchVo.getKeyword());
                browseRecord.setCreateIp(webSearchVo.getCreateIp());
                browseRecord.setUpdateIp(webSearchVo.getCreateIp());
                Result<String> add = browseRecordService.add(browseRecord);
                log.info("延时执行--添修浏览记录结束：" + System.currentTimeMillis());
                if (!add.isFinish()) {
                    log.warn(add.getMsg());
                }
                this.cancel();
            }
        }, 3000);

        return page;
    }

}
